<template>
  <el-dialog
    v-model="visible"
    title="加载草稿"
    width="80%"
    top="5vh"
    destroy-on-close
  >
    <div class="draft-select-container">
      <div class="search-bar">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          style="width: 200px; margin-right: 10px"
          @change="handleSearch"
        />
        <el-input
          v-model="queryParams.dataTemplateName"
          placeholder="请输入模板名称"
          clearable
          style="width: 200px; margin-right: 10px"
          @change="handleSearch"
        />
        <el-button @click="handleSearch" type="primary">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </div>

      <el-table
        :data="tableData"
        v-loading="loading"
        border
        stripe
        style="width: 100%; margin-top: 16px"
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="taskName" label="任务名称" min-width="150" show-overflow-tooltip />
        <el-table-column prop="dataTemplateName" label="模板名称" min-width="150" show-overflow-tooltip />
        <el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            <span>{{ row.remark || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createUserName" label="创建人" width="120" align="center" />
        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="handleLoad(row.id)"
            >
              载入
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="queryParams.page"
          v-model:page-size="queryParams.size"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSearch"
          @current-change="handleSearch"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { selectDataTemplates, deleteDataTemplate } from '@/apis/template/testReport'

const emit = defineEmits(['load'])

const visible = ref(false)
const loading = ref(false)
const tableData = ref<any[]>([])
const total = ref(0)

const queryParams = reactive({
  taskName: undefined,
  dataTemplateName: undefined,
  page: 1,
  size: 10
})

const open = async () => {
  visible.value = true
  await fetchData()
}

const fetchData = async () => {
  try {
    loading.value = true
    const res = await selectDataTemplates(queryParams)
    if (res && res.data) {
      tableData.value = res.data.list || []
      total.value = res.data.total || 0
    } else {
      tableData.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('获取数据模板失败:', error)
    ElMessage.error('获取数据模板失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  queryParams.page = 1
  fetchData()
}

const resetSearch = () => {
  queryParams.taskName = undefined
  queryParams.dataTemplateName = undefined
  queryParams.page = 1
  fetchData()
}

const handleLoad = (id: number) => {
  emit('load', id)
  visible.value = false
}

const handleDelete = async (id: number) => {
  try {
    await ElMessageBox.confirm('确定要删除该模板吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await deleteDataTemplate([id])
    ElMessage.success('删除成功')
    await fetchData()
  } catch (error) {
    console.error('删除失败:', error)
  }
}

defineExpose({ open })
</script>

<style scoped lang="scss">
.draft-select-container {
  padding: 10px;
  
  .search-bar {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    gap: 10px;
    flex-wrap: wrap;
  }
  
  .pagination {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
  }
}

:deep(.el-table) {
  .el-table__header {
    th {
      background-color: #f5f7fa;
      font-weight: 600;
      color: var(--el-text-color-primary);
    }
  }
  
  .el-table__body {
    tr {
      &:hover {
        background-color: #f5f7fa;
      }
    }
  }
}
</style>